//
//  HRDynamicMessageViewController.m
//  Hear
//
//  Created by mac on 2017/5/20.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRDynamicMessageViewController.h"
#import "HRDynamicMessageCell.h"
#import "HRFMDBDynamicNotifacationTool.h"
#import "HRDynamicNotificationModel.h"
#import "HRFollowDyanmicInfoViewController.h"
#import "HRDynamicViewController.h"
#import <DZNEmptyDataSet/UIScrollView+EmptyDataSet.h>

#import "Hear_SingleShowViewController.h" // 状态14的通知展示

@interface HRDynamicMessageViewController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>


@end

@implementation HRDynamicMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.contentArray = [NSMutableArray array];
    
    UIButton *deleteButton = [UIButton buttonWithType:0];
    deleteButton.size = CGSizeMake(60, 40);
    [deleteButton setTitle:@"一键清空" forState:0];
    [deleteButton setTitleColor:[UIColor whiteColor] forState:0];
    deleteButton.titleLabel.font = CustomUIFont(14);
    [deleteButton addTarget:self action:@selector(deleteAllContentList) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:deleteButton];
    self.navigationItem.rightBarButtonItem = rightItem;

//    [self.contentArray addObjectsFromArray:[[HRFMDBDynamicNotifacationTool sharedInstance] selectContentOfType:@"1"]];
//    [self.contentArray addObjectsFromArray:[[HRFMDBDynamicNotifacationTool sharedInstance] selectContentOfType:@"2"]];
//    [self.contentArray addObjectsFromArray:[[HRFMDBDynamicNotifacationTool sharedInstance] selectContentOfType:@"5"]];
//    [self.contentArray addObjectsFromArray:[[HRFMDBDynamicNotifacationTool sharedInstance] selectContentOfType:@"9"]];
//    [self.contentArray addObjectsFromArray:[[HRFMDBDynamicNotifacationTool sharedInstance] selectContentOfType:@"12"]];

    for (NSString *type in self.pushTypeArray) {
        [self.contentArray addObjectsFromArray:[[HRFMDBDynamicNotifacationTool sharedInstance] selectContentOfType:type]];
    }
    if (self.isFromCircle){
        NSMutableArray *unreadContentArr = [NSMutableArray array];
        for (HRDynamicNotificationModel *model in self.contentArray) {
            if (![model.isRead intValue]){
                [unreadContentArr addObject:model];
            }
        }
        [self.contentArray removeAllObjects];
        [self.contentArray addObjectsFromArray:unreadContentArr];
        for (HRDynamicNotificationModel *model in self.contentArray) {
            [[HRFMDBDynamicNotifacationTool sharedInstance] updateSomeField:@"isRead" andValue:@"1" andID:model.ID];
        }
    }

    
    self.tableView = [[HRBaseTableView alloc] initWithFrame:self.view.bounds];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight = 70;
    if (!self.isFromCircle){
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newMessageNotifi) name:@"newMesageNotifi" object:nil];
    }else{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTabItemBageCountNotifi" object:nil];
    }
    
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.emptyDataSetSource = self;
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
    return [UIImage imageNamed:@"通知提示灰色-"];
}


#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.contentArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HRDynamicMessageCell *cell = [HRDynamicMessageCell cellWithTableView:tableView];
    
    HRDynamicNotificationModel *model = self.contentArray[indexPath.row];
    if (model)
    {
        cell.model = model;
    }
    cell.clickIconBlock = ^{
//        if ([model.type intValue]== 2){
            HRDynamicViewController *dynamicVC = [HRDynamicViewController new];
            dynamicVC.currentUserID = model.fromUserID;
            [self.navigationController pushViewController:dynamicVC animated:YES];
//        }
        
    };
    //取消cell的点击状态
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HRDynamicNotificationModel *model = self.contentArray[indexPath.row];
    
    if (![model.isRead intValue]){
        [[HRFMDBDynamicNotifacationTool sharedInstance] updateReadCount:model.ID];
    }
    model.isRead = @"1";
    [self.tableView reloadData];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if ([model.type intValue]== 4 || [model.type intValue]== 9){
        HRDynamicViewController *dynamicVC = [HRDynamicViewController new];
        dynamicVC.currentUserID = model.messageID;
        [self.navigationController pushViewController:dynamicVC animated:YES];
    } else if ([model.type intValue]== 13 || [model.type intValue]== 14) {
        Hear_SingleShowViewController *vc = [Hear_SingleShowViewController new];
        vc.soundID = model.messageID;
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        HRFollowDyanmicInfoViewController *infoVC = [HRFollowDyanmicInfoViewController new];
        infoVC.dynamicID = model.messageID;
        
        [self.navigationController pushViewController:infoVC animated:YES];
    }
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"newMesageNotifi" object:nil];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTabItemBageCountNotifi" object:nil];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        HRDynamicNotificationModel *model = self.contentArray[indexPath.row];
        if ([[HRFMDBDynamicNotifacationTool sharedInstance] deleteRequestId:model.ID]){
            [self.contentArray removeObject:model];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTabItemBageCountNotifi" object:nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"newMesageNotifi" object:nil];
            [tableView reloadEmptyDataSet];
        }
        
    }
    
}

#pragma mark 清空回话列表
- (void)deleteAllContentList{
    for (HRDynamicNotificationModel *model in self.contentArray) {
        [[HRFMDBDynamicNotifacationTool sharedInstance] deleteRequestId:model.ID];
    }
    [self.contentArray removeAllObjects];
    [self.tableView reloadData];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTabItemBageCountNotifi" object:nil];
}


-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}



//-(void)viewWillAppear:(BOOL)animated{
//    [super viewWillAppear:animated];
//    self.hidesBottomBarWhenPushed = YES;
//
//    self.contentArray = [[HRFMDBDynamicNotifacationTool sharedInstance] selectAllContent];
//    [self.tableView reloadData];
//}

- (void)clickBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)newMessageNotifi{
    [self.contentArray removeAllObjects];
//    [self.contentArray addObjectsFromArray:[[HRFMDBDynamicNotifacationTool sharedInstance] selectContentOfType:@"1"]];
//    [self.contentArray addObjectsFromArray:[[HRFMDBDynamicNotifacationTool sharedInstance] selectContentOfType:@"2"]];
//    [self.contentArray addObjectsFromArray:[[HRFMDBDynamicNotifacationTool sharedInstance] selectContentOfType:@"5"]];
//    [self.contentArray addObjectsFromArray:[[HRFMDBDynamicNotifacationTool sharedInstance] selectContentOfType:@"9"]];
    for (NSString *type in self.pushTypeArray) {
        [self.contentArray addObjectsFromArray:[[HRFMDBDynamicNotifacationTool sharedInstance] selectContentOfType:type]];
    }
    
    [self.tableView reloadData];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
